package com.wang.userscenter.interceptors;

import com.wang.userscenter.common.controller.BaseController;
import com.wang.userscenter.common.util.ConstatFinalUtil;
import com.wang.userscenter.users.pojo.Aadmins;
import com.wang.userscenter.users.service.UsersDbService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * 过滤器===保安
 * /back/AdminsBack/main
 */
@Component
public class TicketInterceptor extends BaseController implements HandlerInterceptor{
	@Autowired
	private UsersDbService usersDbService;
	
	/**
	 * 保安要干的事情;
	 * 通过==放行
	 * 不通过==拦截
	 * 
	 * chain:过滤器链
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		log.info("==TicketFilter(验票的保安上场)==:");
		/* 获取session */
		HttpSession session = request.getSession();
		/* 获取对象,此对象是登录的时候放进去;
		 * 如果有,说明登录了.(买票)
		 * 如果没有,说明没有登录,(没有买票,去买票)
		 *  */
		Aadmins admins = (Aadmins) session.getAttribute("adminsSess");
		if (admins != null) {
			return true;
		} else {
			/* 必须买票 */
			String info = ConstatFinalUtil.INFO_JSON.getString(ConstatFinalUtil.INFO_ILLEG_LOGIN);
			session.setAttribute("info", info);
			String loginUrl = request.getContextPath() + "/NoLogin/adminsLogin";
			response.sendRedirect(loginUrl);
			return false;
		}
	}
}
